package pl.krakow.knp.wikiboard.server;


import java.net.ServerSocket;
import java.io.IOException;
import java.util.LinkedList;

/**
 * Class Server
 */
public class Server {
    
    private static final int PORT = 44444;
    private static final int NUMBER_OF_ARGS = 1;
    private static final int MIN_PORT_LENGTH = 5;
    
    private static ServerSocket socket;
    
    /**
     * Create a new instance of Server
     * @param port Define port
     */
    public Server(int port) {
        
        try {
            socket = new ServerSocket(port);
            System.out.println("Wikiboard Server is listening on port " + port);
            System.out.println("Please press CTRL + C to stop the server");
            
            while(true) {
                ServerThread st = new ServerThread( socket.accept() );
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        
        int port = PORT;
        try {
            if(args.length == NUMBER_OF_ARGS && args[0].length() >= MIN_PORT_LENGTH)
                port = Integer.parseInt(args[0]);
            
        } catch (NumberFormatException nfEx) {
            if(nfEx != null) {
                System.out.println(args[0] + " - This isn't the correct port");
                System.out.println("");
            }
        }
        new Server(port);
    }
}
